﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Hangfire;
using KelonProud.Administration.Domain.Authorization;
using KelonProud.BackgroundJobs.Jobs.Base;
using KelonProud.Data;

namespace KelonProud.BackgroundJobs.Jobs
{
    public class TestCronJob:CronJobBase
    {
        public override string CronExpression => "* * * * *";

        public override void Execute()
        {
            var directoryPath = "d://testJob/";

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            using (var session = DatabaseSession.OpenSession())
            {
                   var sql = "select * from KpUsers";

                var query = session.Query<User>(sql);

                Logger.Debug("data:"+query.Count());

                //if (query.Any())
                //{
                //    var file = File.Open(directoryPath + DateTime.Now.GetHashCode() + ".txt", FileMode.OpenOrCreate);
                //    StreamWriter sw = new StreamWriter(file);

                //    query.Each(e =>
                //    {
                //        sw.WriteLine(e.Id+","+e.Name+","+e.UserName+","+e.FullName);
                //    });

                //    sw.Flush();
                //    sw.Dispose();
                //    sw.Close();
                //    file.Dispose();
                //    file.Close();
                //}
            }
        }
    }
}
